<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx事件驱动架构深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 20px;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .concept-card {
            background: white;
            border-radius: 12px;
            padding: 24px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
            border: 1px solid #e5e7eb;
        }
        
        .concept-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0 8px 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 5px;
            top: 20px;
            width: 2px;
            height: calc(100% + 10px);
            background: #e5e7eb;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Nginx事件驱动架构
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    深入探索高性能Web服务器的核心技术
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                        <i class="fas fa-server mr-2"></i>
                        高并发处理
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                        <i class="fas fa-bolt mr-2"></i>
                        事件驱动
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                        <i class="fas fa-cogs mr-2"></i>
                        I/O复用
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">当</span>我们讨论高性能的Web服务器时，Nginx无疑是最受欢迎的选择之一。在许多高并发的应用中，Nginx作为反向代理、负载均衡器以及静态文件服务器，扮演着至关重要的角色。而其高效、稳定的表现，离不开一个关键的技术架构——<strong class="text-purple-600">事件驱动架构</strong>。
            </p>
            <p class="text-lg leading-relaxed text-gray-700 mt-4">
                我们需要理解什么是事件驱动架构，它与传统的多线程或多进程模型有何不同。传统的Web服务器通常依赖于为每个请求分配独立的线程或进程，这样一来，当并发请求量增大时，系统的资源消耗和上下文切换会迅速增加，导致性能瓶颈。而Nginx的事件驱动架构，通过一个单一的进程来处理所有的请求，避免了线程的过度切换和内存的浪费，实现了高效的I/O处理。
            </p>
        </div>

        <!-- Architecture Overview Diagram -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">架构概览</h2>
            <div class="mermaid">
                graph TB
                    A[客户端请求] --> B[Master进程]
                    B --> C[Worker进程1]
                    B --> D[Worker进程2]
                    B --> E[Worker进程3]
                    B --> F[Worker进程4]
                    
                    C --> G[事件循环]
                    D --> H[事件循环]
                    E --> I[事件循环]
                    F --> J[事件循环]
                    
                    G --> K[epoll/kqueue]
                    H --> K
                    I --> K
                    J --> K
                    
                    K --> L[非阻塞I/O]
                    
                    style A fill:#f9f,stroke:#333,stroke-width:2px
                    style B fill:#bbf,stroke:#333,stroke-width:2px
                    style K fill:#bfb,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Event-Driven Architecture Section -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">事件驱动架构简介</h2>
            
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                事件驱动架构（Event-driven Architecture，EDA）是一种通过事件来驱动系统行为的架构模式，广泛应用于高并发、高性能的应用场景。与传统的线程模型不同，事件驱动架构通过监听、捕获和响应事件来实现系统操作。
            </p>

            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="concept-card">
                    <div class="feature-icon">
                        <i class="fas fa-broadcast-tower"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">事件源</h3>
                    <p class="text-gray-600">产生事件的实体，如客户端请求、传感器变化等</p>
                </div>
                <div class="concept-card">
                    <div class="feature-icon">
                        <i class="fas fa-satellite-dish"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">事件监听器</h3>
                    <p class="text-gray-600">监听并处理事件的组件，通常是一个或多个事件处理器</p>
                </div>
                <div class="concept-card">
                    <div class="feature-icon">
                        <i class="fas fa-sync-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">事件循环</h3>
                    <p class="text-gray-600">负责持续监听事件，并在事件发生时触发相关处理操作</p>
                </div>
            </div>

            <div class="highlight-box">
                <h4 class="text-2xl font-bold mb-3">
                    <i class="fas fa-lightbulb mr-2"></i>核心优势
                </h4>
                <ul class="space-y-2">
                    <li><i class="fas fa-check-circle mr-2"></i>高并发支持：单线程处理成千上万的并发连接</li>
                    <li><i class="fas fa-check-circle mr-2"></i>低内存开销：无需为每个请求创建线程或进程</li>
                    <li><i class="fas fa-check-circle mr-2"></i>高效的I/O处理：非阻塞I/O操作，避免资源浪费</li>
                    <li><i class="fas fa-check-circle mr-2"></i>可扩展性强：通过增加worker进程轻松应对更高负载</li>
                </ul>
            </div>
        </div>

        <!-- Event Module Section -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">Nginx事件模块</h2>
            
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                在Nginx的架构中，事件模块是其高性能的关键组件之一。它负责管理和分发I/O事件，决定如何高效地处理大量并发请求。通过事件模块，Nginx能够以极低的资源消耗和高并发处理能力，响应来自客户端的请求。
            </p>

            <div class="bg-gray-50 rounded-xl p-6 mb-6">
                <h3 class="text-xl font-bold mb-4">
                    <i class="fas fa-cog mr-2 text-purple-600"></i>配置示例
                </h3>
                <div class="code-block">
                    <pre>events {
    worker_connections 1024;    # 每个worker进程的最大连接数
    use epoll;                 # Linux下使用epoll事件模型
    multi_accept on;           # 允许worker进程同时接受多个连接
}</pre>
                </div>
            </div>

            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-purple-50 rounded-xl p-6">
                    <h4 class="text-lg font-bold mb-3 text-purple-800">
                        <i class="fas fa-tasks mr-2"></i>核心组件
                    </h4>
                    <ul class="space-y-2 text-gray-700">
                        <li><strong>master进程：</strong>负责启动、管理worker进程</li>
                        <li><strong>worker进程：</strong>处理客户端请求的进程</li>
                        <li><strong>事件驱动模型：</strong>基于系统调用的异步I/O处理</li>
                    </ul>
                </div>
                <div class="bg-blue-50 rounded-xl p-6">
                    <h4 class="text-lg font-bold mb-3 text-blue-800">
                        <i class="fas fa-chart-line mr-2"></i>性能优化
                    </h4>
                    <ul class="space-y-2 text-gray-700">
                        <li>增加worker进程数量</li>
                        <li>调整worker_connections参数</li>
                        